package com.fujica.invoicing.service.impl;

import com.fujica.invoicing.entity.TGoods;
import com.fujica.invoicing.entity.TGoodsList;
import com.fujica.invoicing.entity.TGoodsListDetail;
import com.fujica.invoicing.mapper.IGoodsListDetailMapper;
import com.fujica.invoicing.mapper.IGoodsListMapper;
import com.fujica.invoicing.mapper.IGoodsMapper;
import com.fujica.invoicing.request.goods.GoodsDetailReq;
import com.fujica.invoicing.request.goods.GoodsListReq;
import com.fujica.invoicing.service.IGoodsService;
import com.fujica.invoicing.utils.Result;
import com.fujica.invoicing.utils.ResultBody;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * @ClassName GoodsServiceImpl
 * @Description TODO
 * @Author admin
 * @Date 2020/8/11 14:08
 * @Version 1.0
 **/
@Service
@Slf4j
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private IGoodsMapper iGoodsMapper;

    @Autowired
    private IGoodsListMapper iGoodsListMapper;

    @Autowired
    private IGoodsListDetailMapper iGoodsListDetailMapper;

    /**
     * 获取商品列表
     * @param
     * @return
     */
    @Override
    public Result list() {

        List<TGoods> goodsList = iGoodsMapper.selectAll();
        // 根据商店id获取
        return ResultBody.ok(goodsList);
    }

    /**
     * 获取单个商品在对应门店的商品清单中的商品详情
     * @param
     * @return
     */
    @Override
    public Result detail(Integer goodsId, Integer storeId) {

        Map goodsListMap = Maps.newHashMap();
        goodsListMap.put("sId", storeId);

        // 查找这个门店对应的商品清单
        Integer listId = iGoodsListMapper.list(goodsListMap).stream()
                .sorted(Comparator.comparing(TGoodsList::getUpdate).reversed()) // 倒序排序 根据更新时间排序
                .findFirst().get().getId();


        Map goodsListDetailMap = Maps.newHashMap();
        goodsListDetailMap.put("listId", listId);
        goodsListDetailMap.put("gId", goodsId);


        // 根据商品清单id查找清单列表
        List<TGoodsListDetail> goodsList = iGoodsListDetailMapper.list(goodsListDetailMap);
        if (goodsList.size() > 0) {
            TGoodsListDetail goodsListDetail = goodsList.get(0);
            return ResultBody.ok(goodsListDetail);
        }

        return ResultBody.ok("没有相关商品");
    }
}
